library(tidyverse) # for data cleaning and plotting
library(gardenR) # for Lisa's garden data
library(lubridate) # for date manipulation
library(openintro) # for the abbr2state() function
library(palmerpenguins)# for Palmer penguin data
library(maps) # for map data
library(ggmap) # for mapping points on maps
library(gplots) # for col2hex() function
library(RColorBrewer) # for color palettes
library(sf) # for working with spatial data
library(leaflet) # for highly customizable mapping
library(ggthemes) # for more themes (including theme_map())
library(plotly) # for the ggplotly() - basic interactivity
library(gganimate) # for adding animation layers to ggplots
library(transformr) # for "tweening" (gganimate)
library(gifski) # need the library for creating gifs but don't need to load each time
library(shiny) # for creating interactive apps
theme_set(theme_minimal())
# SNCF Train data
small_trains <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/small_trains.csv")
# Lisa's garden data
data("garden_harvest")
# Lisa's Mallorca cycling data
mallorca_bike_day7 <- read_csv("https://www.dropbox.com/s/zc6jan4ltmjtvy0/mallorca_bike_day7.csv?dl=1") %>%
select(1:4, speed)
# Heather Lendway's Ironman 70.3 Pan Am championships Panama data
panama_swim <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_swim_20160131.csv")
panama_bike <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_bike_20160131.csv")
panama_run <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_run_20160131.csv")
#COVID-19 data from the New York Times
covid19 <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
Go here or to previous homework to remind yourself how to get set up.
Once your repository is created, you should always open your project rather than just opening an .Rmd file. You can do that by either clicking on the .Rproj file in your repository folder on your computer. Or, by going to the upper right hand corner in R Studio and clicking the arrow next to where it says Project: (None). You should see your project come up in that list if you’ve used it recently. You could also go to File –> Open Project and navigate to your .Rproj file.
Put your name at the top of the document.
For ALL graphs, you should include appropriate labels.
Feel free to change the default theme, which I currently have set to theme_minimal().
Use good coding practice. Read the short sections on good code with pipes and ggplot2. This is part of your grade!
NEW!! With animated graphs, add eval=FALSE to the code chunk that creates the animation and saves it using anim_save(). Add another code chunk to reread the gif back into the file. See the tutorial for help.
When you are finished with ALL the exercises, uncomment the options at the top so your document looks nicer. Don’t do it before then, or else you might miss some important warnings and messages.
ggplotly() function.tomato_total_harvest <- garden_harvest %>%
filter(vegetable == "tomatoes") %>%
mutate(ordered_variety = fct_reorder(variety, date, min),
weight_in_pounds = weight * 0.00220462) %>%
group_by(ordered_variety) %>%
summarize(total_harvest = sum(weight_in_pounds)) %>%
ggplot() +
geom_col(aes(x = total_harvest, y = ordered_variety,
text = ordered_variety)) +
labs(y = "",
x = "",
title = "The total harvests for tomato variety")
ggplotly(tomato_total_harvest,
tooltip = c("text", "x"))
beats_harvest <- garden_harvest%>%
filter(vegetable == "beets") %>%
group_by(variety, date) %>%
summarize(totalWeight = sum(weight)) %>%
mutate(weightInPounds = totalWeight * 0.00220462) %>%
mutate(cum_sum_weight = cumsum(weightInPounds)) %>%
ggplot() +
geom_line(aes(y = cum_sum_weight, x = date, color = variety,
text = cum_sum_weight)) +
scale_color_manual(values = c("Gourmet Golden" = "darkgoldenrod1", "Sweet Merlin" = "blueviolet", "leaves" = "green"))
ggplotly(beats_harvest,
tooltip = c("text", "x"))
small_trains dataset that contains data from the SNCF (National Society of French Railways). These are Tidy Tuesday data! Read more about it here.small_trains
small_trains %>%
group_by(departure_station) %>%
arrange(desc(total_num_trips))
departure_trips <- drop_na(small_trains) %>%
filter(year == 2017) %>%
group_by(month, departure_station) %>%
summarise(month_trip = sum(total_num_trips)) %>%
top_n(n = 10, wt = month_trip) %>%
arrange(month, month_trip) %>%
mutate(rank = 1: n()) %>%
ggplot(aes(y = factor(rank), x = month_trip,
fill = departure_station)) +
geom_col() +
geom_text(aes(label = departure_station),
color = "black", hjust = "left", x = -10) +
labs(title = "Change in trips per month in 2017",
subtitle = "Month: {frame_time}",
x = "",
y = "") +
scale_fill_viridis_d() +
theme(axis.line = element_blank(),
panel.grid = element_blank(),
axis.text.y = element_blank(),
legend.position = "none") +
transition_time(month)
animate(departure_trips, nframes = 200, duration = 30)
## Error in animate(departure_trips, nframes = 200, duration = 30): object 'departure_trips' not found
anim_save("departure_race.gif")
## Error: The animation object does not specify a save_animation method
geom_area() examples here). You will look at cumulative harvest of tomato varieties over time. You should do the following:garden_harvest data, filter the data to the tomatoes and find the daily harvest in pounds for each variety.fct_reorder()) from most to least harvested (most on the bottom).I have started the code for you below. The complete() function creates a row for all unique date/variety combinations. If a variety is not harvested on one of the harvest dates in the dataset, it is filled with a value of 0.
tomatoes_variety <- garden_harvest %>%
filter(vegetable == "tomatoes") %>%
group_by(date, variety) %>%
summarize(daily_harvest_lb = sum(weight)*0.00220462) %>%
ungroup() %>%
complete(variety, date, fill = list(daily_harvest_lb = 0)) %>%
group_by(variety) %>%
mutate(cum_harvest_lb = cumsum(daily_harvest_lb)) %>%
mutate(variety = fct_reorder(variety, cum_harvest_lb))
tomatoes_variety %>%
ggplot(aes(x = date,
y = cum_harvest_lb,
fill = variety)) +
geom_area() +
#geom_text(aes(label = variety)) +
labs(title = "Cumulative harvest (lb)",
subtitle = "Date: {frame_along}",
x = "",
y = "",
color = "vegetable") +
theme(legend.position = "right") +
transition_reveal(date)
anim_save("tomatoes_variety.gif")
## Error: The animation object does not specify a save_animation method
mallorca_bike_day7 bike ride using animation! Requirements:ggmap.ggimage package and geom_image to add a bike image instead of a red point. You can use this image. See here for an example.library(ggimage)
mallorca_map <- get_stamenmap(
bbox = c(left = 2.38, bottom = 39.55, right = 2.62, top = 39.7),
maptype = "terrain",
zoom = 10
)
mallorca_bike <- mallorca_bike_day7 %>%
mutate(bike = "https://raw.githubusercontent.com/llendway/animation_and_interactivity/master/bike.png")
ggmap(mallorca_map) +
geom_path(data = mallorca_bike,
aes(x = lon, y = lat, color = ele),
size = 0.8) +
geom_image(data = mallorca_bike,
aes(x = lon, y = lat, image = bike),
size = 0.05) +
theme_map() +
labs(title = "Biking in Mallorca",
subtitle = "Time: {frame_along}",
x = "",
y = "") +
transition_reveal(time)
anim_save("mallorca_biking_with_bike.gif")
## Error: The animation object does not specify a save_animation method